//
//  SISSMessageViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMessageViewController.h"
#import "SISSNotificationCell.h"
#import "SISSNotificationListViewController.h"
#import "SISSConversationListViewController.h"
#import "SISSNetworkingManager+Message.h"
#import "SISSLatestNotificationModel.h"

static NSString *notificatonCellReuseID = @"notificatonCellReuseID";
@interface SISSMessageViewController ()<UITableViewDelegate,UITableViewDataSource>


/* tableView */
@property (weak, nonatomic) UITableView *tableView;
/* 咨询控制器 */
@property (weak, nonatomic) SISSConversationListViewController *contactListViewController;

/* 最新协会公告模型 */
@property (strong, nonatomic) SISSLatestNotificationModel *noticeRecordModel;
/* 最新系统通知模型 */
@property (strong, nonatomic) SISSLatestNotificationModel *systemMessageModel;

@end

@implementation SISSMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    } // 去除顶部20个距离
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf requestNotificationData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestNotificationData];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:notificatonCellReuseID];
    SISSLatestNotificationModel *model = [[SISSLatestNotificationModel alloc] init];
    if (indexPath.row == 0) {
        model.iconName = @"公告";
        model.headline = @"协会公告";
        cell.latestNotiModel = self.noticeRecordModel ? self.noticeRecordModel : model;
    } else if (indexPath.row == 1) {
        model.iconName = @"通知";
        model.headline = @"系统通知";
        cell.latestNotiModel = self.systemMessageModel ? self.systemMessageModel : model;
    }
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSNotificationListViewController *vc = [[SISSNotificationListViewController alloc] init];
    
    vc.title = indexPath.row == 0 ? @"协会公告" : @"系统通知";
    vc.notificationType = indexPath.row == 0 ? SISSNotificationTypeNoticeRecord : SISSNotificationTypeSystemMessage;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

#pragma mark - 加载数据
- (void)requestNotificationData {
    
    WeakSelf
    [kNetworkManager requestLatestNoticeRecordWithSuccess:^(SISSLatestNotificationModel *noticeRecordModel) {
        
        noticeRecordModel.iconName = @"公告";
        noticeRecordModel.headline = @"协会公告";
        weakSelf.noticeRecordModel = noticeRecordModel;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf.tableView reloadData];
        }];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    [kNetworkManager requestLastSystemMessageWithSuccess:^(SISSLatestNotificationModel *systemMessageModel) {
        
        systemMessageModel.iconName = @"通知";
        systemMessageModel.headline = @"系统通知";
        weakSelf.systemMessageModel = systemMessageModel;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView.mj_header endRefreshing];
            [weakSelf.tableView reloadData];
        }];
        
    } failed:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UISegmentedControl点击事件
- (void)segmentControlbutClick:(UISegmentedControl *)sender {
    

    if (sender.selectedSegmentIndex == 0) {
        
        self.contactListViewController.view.hidden = NO;
        [self.contactListViewController requestContactListData];
    } else {

        self.contactListViewController.view.hidden = YES;

    }
}

#pragma mark - NavigationItem
- (void)setupNavigationItems {
    
    [super setupNavigationItems];
    
    [self.navigationController.navigationBar setBackgroundImage:[ImageWithColor imageWithColor:COLOR(0xF9F9F9)] forBarMetrics:UIBarMetricsDefault];
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@" 咨询 ", @" 通知 "]];
    segmentControl.selectedSegmentIndex = 0;
    segmentControl.tintColor = COLOR(0x249DC5);
    [segmentControl addTarget:self action:@selector(segmentControlbutClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentControl;
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    [tableView registerClass:[SISSNotificationCell class] forCellReuseIdentifier:notificatonCellReuseID];
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // 添加咨询视图
    SISSConversationListViewController *contactListVC = [[SISSConversationListViewController alloc] init];
    [self.view addSubview:contactListVC.view];
    [self addChildViewController:contactListVC];
    contactListVC.view.frame = CGRectOffset(self.view.frame, 0, 0);
    [contactListVC didMoveToParentViewController:self];
    
    self.contactListViewController = contactListVC;
    self.contactListViewController.view.hidden = NO;
}


@end
